species_name_list=['Tomato','Potato','viarum','lyratum','melongena','Pepper','Golden','Wolfberry','Datura','biflora']
import sys

for i in species_name_list:
    fa_bed=open(i+'_inter.fa.bed','r')
    bed_dict={}
    for line in fa_bed:
        line_list=line.strip().split('\t')
        bed_dict[line_list[-1]]=line_list[:-1]
    pseud_file=open(i+'_pseudgene','r')
    out_body_bed=open(i+'loss_body.bed','w')
    out_flank_bed=open(i+'loss_flank.bed','w')
    print(i)
    for line in pseud_file:
        line_list=line.strip().split('\t')
        start=int(bed_dict[line_list[2]][1])
        end=int(bed_dict[line_list[2]][2])
        out_body_bed.write(bed_dict[line_list[2]][0]+'\t'+str(start+int(line_list[-2]))+'\t'+str(start+int(line_list[-1]))+'\n')
        if int(line_list[-2])<2000:
            if int(line_list[-2])<10:
                x=1
                # continue
            else:
                out_flank_bed.write(bed_dict[line_list[2]][0]+'\t'+str(start)+'\t'+str(start+int(line_list[-2]))+'\n')
                
        else:
            out_flank_bed.write(bed_dict[line_list[2]][0]+'\t'+str(start+int(line_list[-2])-2000)+'\t'+str(start+int(line_list[-2]))+'\n')
            
        
        if (end-start)-int(line_list[-1])<2000:
            if (end-start)-int(line_list[-1])<10:
                x=1
            else:
                out_flank_bed.write(bed_dict[line_list[2]][0]+'\t'+str(start+int(line_list[-1]))+'\t'+str(end)+'\n')
                
        else:
            out_flank_bed.write(bed_dict[line_list[2]][0]+'\t'+str(start+int(line_list[-1]))+'\t'+str(start+int(line_list[-1])+2000)+'\n')
            
                
            
            
                
                
            
    
    